

#include "gsmio.h"
//#include <iostream.h>
#include <string.h>
#include "FreeRTOS.h"
#include "task.h"
#include "croutine.h"
#include "uart.h"

#ifdef __cplusplus
extern "C" {
#endif
#pragma location = "EMAC_DMA_RAM"
  char word[512]="";

#ifdef __cplusplus
}
#endif
//static const char* GSMIO_HTTP_STR  = "Content-Length: ";
   
//static const int   GSMIO_HTTP_STRL = 16;
#pragma location = "EMAC_DMA_RAM"
unsigned char sd_WARD[2] = "";
#pragma location = "EMAC_DMA_RAM"
  char SD_WARD2[2]="";


//static const char* GSMIO_CRC32_STR  = "X-CRC: ";
//static const int   GSMIO_CRC32_STRL = 7;

static unsigned int gsmio_tcp_receive(unsigned long* timeout_ms);

GSM_CMD_t gsmio_command(char* cmd, char* resp, int timeout_ms)
{
  return gsmio_command_n(cmd, strlen(resp), resp, timeout_ms, 1);
}

int OPROSS()
{
  int opross=0;
  //#pragma location = "EMAC_DMA_RAM"
  char temp[64] = "";
  
 UART_READ(&temp,2,"0",1);
 
  //modem_debug(temp);
  
   if (strcmp(&temp,"OK")==0)
         {
            opross=1;
            return opross;
         }
 
  if (strcmp(&temp,"T")==0)
         {
           opross=1;
            return opross;
         }
  
    if (strcmp(&temp,"UP")==0)
         {
           opross=0;
            return opross;
         }
 
 return opross;
}




GSM_CMD_t gsmio_command_n(char* cmd, int len, char* resp, int timeout_ms, char flush)
{

  
  int UAi=0;
  //WDT_RESET();
   // memset(word, 0x00, 64); 
 // UART_CLEAR();
  modem_puts(cmd);
  //modem_debug(cmd);
  vTaskDelay(100);      
  UAi=UART_READ(&word,len,resp,timeout_ms);
  //modem_debug("\r\n");
  if (UAi==7)
         {
            return GSM_CMD_OK;
         }      
  return GSM_CMD_TIMEOUT; 
      
       /*
  while (1){
    char data = 0;
    if (timeout_ms <= 0) 
      return GSM_CMD_TIMEOUT;
          
    if (!modem_read(&data, 1)){
  //    ctl_timeout_wait(ctl_get_current_time()+ctl_get_ticks_per_second()/100);
      timeout_ms -= 10;
      continue;
    }

    if (data=='>' && strcmp(">", resp)==0) return GSM_CMD_OK;
    if (data=='\r' || data=='\n') {
      word[j] = 0;
  //    debug_printf("%s\r\n", word);
      if (j > 0 && strcmp(word, resp) == 0){
        return GSM_CMD_OK;
      }
      j = 0;
      continue;
    }
    word[j] = data;
    j += 1;
  }*/
}

SD_CMD_t SD_COMP(char* sd_WARD)
{
SD_WARD2[0]=sd_WARD[0];
SD_WARD2[1]=sd_WARD[1];
  
       if (strcmp(SD_WARD2,"28") == 0)
       {
          return SD_CMD_OK;
      }
       return SD_CMD_ERROR;
}

unsigned int gsmio_tcp_receive(unsigned long* timeout_ms)
{
  unsigned int size = 0; // Bytes to read
  char s = 0; // State: 0 - '+', 1 - 'I', 2 - 'P', 3- 'D', 4 - length and ':', 6 - data
  char c = 0; // Input char from modem

  while (*timeout_ms){
    if (!modem_read(&c, 1)) {
   //   ctl_timeout_wait(ctl_get_current_time()+ctl_get_ticks_per_second()/1000);
      *timeout_ms -= 1;
      continue;
    }
    switch(s) {
      case 0:
        if (c == '+') s = 1;
      break;
      case 1:
        if (c == 'I') s = 2;
        else if (c == '+') s = 1;
        else s = 0;
      break;
      case 2:
        if (c == 'P') s = 3;
        else s = 0;
      break;
      case 3:
        if (c == 'D') s = 4;
        else s = 0;
      break;
      case 4:
        if (c == ',') s = 5;
        else s = 0;
      break;
      case 5:
        if (c >= '0' && c <= '9') size = size*10 + (c - '0');
        else if (c == ':') 
          return size;
        else s = 0;
    }
  } 
  return 0;
}

GSM_CMD_t gsmio_json_receive(unsigned long timeout_ms, json_node** output)
{
  char crypt[8] = ""; // 64-bit crypt block 
  unsigned char block = 0; // Current block position

  json_parse_init();
  unsigned int btr = gsmio_tcp_receive(&timeout_ms); // Bytes to read
  while (timeout_ms && btr){
    if (!modem_read(&crypt[block], 1)) {
   //   ctl_timeout_wait(ctl_get_current_time()+ctl_get_ticks_per_second()/1000);
      timeout_ms -= 1;
      continue;
    }   
    block++;

    if (block >= 8){ //Block complete, decrypt...
      block = 0;
      rtea_decrypt_block((unsigned int*)crypt, RTEA_KEY);
      for (int i = 0; i < 8; i++){
        if (json_parse_char(crypt[i], output)) 
          return GSM_CMD_OK;
      }
    }
    if (--btr == 0) break;
  } // while (timeout_ms)...

  if (timeout_ms > 0 && btr > 0) // Timeout and chars left
    return GSM_CMD_TIMEOUT;

  return GSM_CMD_ERROR;
}

GSM_CMD_t gsmio_http_receive(FIL* file, unsigned int *crc32, unsigned long timeout_ms)
{
 
  return GSM_CMD_OK;
}

void gsmio_power_on()
{
  GSMIO_PULLUPS_OFF();
//  MODEM_ENABLE_RXD_PIN(); // Enable only RXD pin to avoid "phantom-power"
 // MODEM_CLR_RTS();        // Clear GSM modem RTS to allow receive data
  
  
  GSMIO_POWER_ON();
  
  vTaskDelay(100); 
}

void gsmio_power_off()
{
  GSMIO_POWER_OFF();
  vTaskDelay(1000); 
}

void gsmio_on()
{
 // GSMIO_CLR_KEY();
  // vTaskDelay(1500); 
 // delay_ms(1500);
 // GSMIO_SET_KEY();
  
 //  FIO2CLR_bit.P2_9 = 1;   //gsm_sim_1  
 // MODEM_ENABLE_TXD_PIN();
 // MODEM_ENABLE_CTS_PIN();
 //if (gsmio_command("ATE0\r\n", "OK", 1000) != GSM_CMD_OK) 
   
  
// if (gsmio_command("AT\r\n", "OK", 1000) == GSM_CMD_OK) 
     // break;
  
   gsmio_command("ATE0\r\n", "OK", 1000); 
   gsmio_command("AT+IPR=19200\r\n", "OK", 1000);
  gsmio_command("AT&W\r\n", "OK", 1000); 
   gsmio_command("", "Call Ready", 20000); 
   
  //MODEM_ENABLE_TXD_PIN();
  //MODEM_ENABLE_CTS_PIN();
}
